var Icd = require('../models/icd.js');
var Ccd = require('../models/ccd.js');
var icdIndex = require('../models/icdIndex.js');
var ccdIndex = require('../models/ccdIndex.js');
var ObjectID = require("mongodb").ObjectID;
//icd
exports.icd = function(req, res) {
    console.log("dataIcd");
    console.log(req.body.user);
    console.log(req.body.id)
    var filter = {};
    filter["user"] = req.body.user;
    filter["ccd_id"] = ObjectID(req.body.id);
    Icd.get(filter,function(err,doc){
        console.log(doc);
        res.send({icd:doc});
    });
};

exports.icdElement = function(req, res) {
    switch(req.body['process'])
    {
        case 'add':
            icdAdd(req,res);
            break;
        case 'remove':
            icdRemove(req,res);
            break;
        case 'revise':
            icdRevise(req,res);
            break;
    }
};

icdAdd = function(req,res){
    console.log("Icd add");
    var addType = req.body['type'];

    req.body['statusArray'][addType] = req.body['data']['new'];
    Icd.generateFilter(req.body['type'],req.body['statusArray'],function(filter){
        switch(addType){
            case 'class':
            case 'relation':
            case 'attribute':
            case 'attributeElemValue':
                    Icd.add2(filter,function(err,count){
                        Icd.get(filter[0],function(err,doc){
                            res.send({count:count,icd:doc});
                            filter[0] = {'_id':ObjectID(req.body['statusArray']['ccd_id'])};
                            Ccd.add2(filter,function(err,count){});
                        });
                    });
                    break;
            default :Icd.add(filter,function(err,count){
                        Icd.get(filter[0],function(err,doc){
                            res.send({count:count,icd:doc});
                            console.log(filter);
                            filter[0] = {'_id':ObjectID(req.body['statusArray']['ccd_id'])};
                            Ccd.add(filter,function(err,count){});
                        });
                    });
                    break;
        }
    });
};

icdRemove = function(req,res){
    console.log("Icd remove");
    var removeType = req.body['type'];
    req.body['statusArray'][removeType] = req.body['data']['new'];

    Icd.generateFilter(req.body['type'],req.body['statusArray'],function(filter){
        if(removeType == 'cd'){
            //console.log(filter);
            filter[2] = {};
            filter[2]["user"] = req.session.user.mail;

            filter[3] = {"$unset":{}};
            filter[3]["$unset"][req['body']['statusArray']["ccd_id"]] = "";
            //filter[3]["$unset"] =  req.body['statusArray']["ccd_id"];
            Icd.remove(filter,function(){
                console.log("1");
                res.send({});
                console.log("2");
            });
        }
        else{
            Icd.delete(filter,function(err,count){
                Icd.get(filter[0],function(err,doc){
                    Ccd.generateSubFilter(req.body['data']['sub'],function(subFilter){
                        var ccdFilter = {};
                        for(var i=0;i<subFilter.length;i++){
                            ccdFilter[filter[1]+subFilter[i]] = -1;
                        }
                        //console.log(ccdFilter);
                        Ccd.ref( {'_id':ObjectID(req.body['statusArray']['ccd_id'])},ccdFilter,function(err,number){
                        });
                    });
                    return res.send({count:count,icd:doc});
                })
            });
        };


    });
};



icdRevise = function(req,res){
    console.log("Icd revise");

    var reviseType = req.body['type'];
    switch(reviseType){
        case '_description':
            Icd.generateElement(req.body['type'],req.body['statusArray'],null,req.body['data']['sub'],function(id,element){
                //console.log(element);
                Icd.copy(id,element,function(err,count){
                    Icd.get(id,function(err,doc){
                        res.send({count:count,icd:doc});
                    })
                });
            });
            break;

        default://cd class relation attribute
            req.body['statusArray'][reviseType] = req.body['data']['old'];
            if(req.body['data']['old'] != ""){
                Icd.generateFilter(req.body['type'],req.body['statusArray'],function(filter){
                    Icd.delete(filter,function(err,count){
                        console.log(count);
                    });
                    Ccd.generateSubFilter(req.body['data']['sub'],function(subFilter){
                        var ccdFilter = {};

                        for(var i=0;i<subFilter.length;i++){
                            ccdFilter[filter[1]+subFilter[i]] = -1;
                            //if(reviseType==='attributeElemValue') ccdFilter[filter[2]+subFilter[i]] = -1;;
                        }
                        //console.log("--------");
                        //console.log(ccdFilter);
                        Ccd.ref( {'_id':ObjectID(req.body['statusArray']['ccd_id'])},ccdFilter,function(err,number){
                        });
                    });
                });
            }

            Icd.generateElement(req.body['type'],req.body['statusArray'],req.body['data']['new'],req.body['data']['sub'],function(id,element){
                Icd.copy(id,element,function(err,count){
                    Icd.get(id,function(err,doc){
                        res.send({count:count,icd:doc});
                    })
                });
                Ccd.generateSubFilter(req.body['data']['sub'],function(subFilter){
                    var ccdFilter = {};
                    for(var filter in element){
                        break;
                    }
                    //console.log(filter);
                    for(var i=0;i<subFilter.length;i++){
                        ccdFilter[filter+subFilter[i]] = 1;
                    }
                    //console.log("+++++++");
                    //console.log(ccdFilter);
                    Ccd.ref( {'_id':ObjectID(req.body['statusArray']['ccd_id'])},ccdFilter,function(err,number){
                    });
                });
            });

            break;
    }
};

//ccd
exports.ccd = function(req, res) {
    console.log("dataCcd");
    Ccd.get({_id:ObjectID(req.body['id'])},function(err,doc){
        res.send({ccd:doc});
    });
};

exports.index = function(req,res){
    console.log("dataIndex");
    var filter = {};
    var icd_index=[];
    var ccd_index;
    filter["user"] = req.body.user;
    icdIndex.get(filter,function(err,doc){
        var exist = [];
        for(var i= 0;i<doc.length;i++){}
        for(var key in doc[0]){
            if(key != 'user' && key != '_id'){
                console.log("push key");
                exist.push(ObjectID(key));
                icd_index.push(doc[0][ObjectID(key)]);
            }
        }
        var filter = {};
        filter["ccd_id"] ={};
        filter["ccd_id"]["$nin"] = exist;

        ccdIndex.get(filter,function(err,doc){
            ccd_index = doc;
            console.log("*************");
            console.log(icd_index);
            console.log(ccd_index);
            //console.log(icd_index[0].last_time.toDateString());
            res.send({icd_index:icd_index,ccd_index:ccd_index});
        });
    });
};